define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'litestore/ordergoods/index' + location.search,
                    add_url: 'litestore/ordergoods/add',
                    edit_url: 'litestore/ordergoods/edit',
                    del_url: 'litestore/ordergoods/del',
                    multi_url: 'litestore/ordergoods/multi',
                    import_url: 'litestore/ordergoods/import',
                    table: 'litestore_order_goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'goods_id', title: __('Goods_id')},
                        {field: 'goods_name', title: __('Goods_name'), operate: 'LIKE'},
                        {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'specValue', title: __('规格值')},
                        {field: 'goods_price', title: __('Goods_price'), operate:'BETWEEN'},
                        {field: 'total_num', title: __('Total_num')},
                        {field: 'total_price', title: __('总价格'), operate:'BETWEEN'},
                        {field: 'order_id', title: __('订单ID')},
                        {field: 'supplier.name', title: __('供应商')},
                        {field: 'kd_num', title: __('快递单号'), operate: 'LIKE'},
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'list',
                                    title: __('发起售后'),
                                    text: '发起售后',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: function (row) {
                                        return "litestore/supplierorderservice/add?id=" + row['id'];
                                    }
                                },
                                {
                                    name: 'list',
                                    title: __('查看物流'),
                                    icon: 'fa fa-eye',
                                    text: '查看物流',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: function (row) {
                                        return "order/shoporder/add?id=" + row['id'];
                                    }
                                },

                            ],
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-edit", null);
                                $(table).data("operate-del", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
